package com.ala4.oxcafe.repository;

import com.ala4.oxcafe.boot.page.PageDomain;
import com.ala4.oxcafe.common.utils.text.StrUtil;
import com.ala4.oxcafe.entity.RegionInfo;
import com.ala4.oxcafe.repository.mapper.RegionInfoMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 系统附件存储实体
 *
 * @author PING
 * @date 2025-03-25
 */
@Repository
public class RegionInfoRepository extends ServiceImpl<RegionInfoMapper, RegionInfo> implements IService<RegionInfo> {

    private LambdaQueryWrapper<RegionInfo> queryParam(RegionInfo regionInfo) {

        LambdaQueryWrapper<RegionInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.and(StrUtil.isNotEmpty(regionInfo.getSearchValue()), item -> {
            item.like(RegionInfo::getRegionCode, regionInfo.getSearchValue())
                    .or().like(RegionInfo::getRegionName, regionInfo.getSearchValue());
        });
        queryWrapper.eq(StrUtil.isNotEmpty(regionInfo.getRegionCode()), RegionInfo::getRegionCode, regionInfo.getRegionCode());
        queryWrapper.lt(StrUtil.isNotEmpty(regionInfo.getLessThanRegionLevel()), RegionInfo::getRegionLevel, regionInfo.getLessThanRegionLevel());
        queryWrapper.eq(StrUtil.isNotEmpty(regionInfo.getRegionParent()), RegionInfo::getRegionParent, regionInfo.getRegionParent());

        return queryWrapper;
    }

    public IPage<RegionInfo> selectRegionInfoPage(PageDomain pageDomain, RegionInfo regionInfo) {

        return this.page(pageDomain.toPlusPage(), queryParam(regionInfo));
    }

    public List<RegionInfo> selectRegionInfoList(RegionInfo regionInfo) {
        return this.list(queryParam(regionInfo));
    }
}
